package com.cc.jdframedemo;

import com.cc.jdframedemo.domain.Student;
import io.github.burukeyou.dataframe.iframe.SDFrame;
import io.github.burukeyou.dataframe.iframe.item.FI2;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Test1 {
    static List<Student> studentList = new ArrayList<>();

    static {
        studentList.add(new Student(1, "a", "一中", "一年级", 11, new BigDecimal(1)));
        studentList.add(new Student(2, "a", "一中", "一年级", 11, new BigDecimal(1)));
        studentList.add(new Student(3, "b", "一中", "三年级", 12, new BigDecimal(2)));
        studentList.add(new Student(4, "c", "二中", "一年级", 13, new BigDecimal(3)));
        studentList.add(new Student(5, "d", "二中", "一年级", 14, new BigDecimal(4)));
        studentList.add(new Student(6, "e", "三中", "二年级", 14, new BigDecimal(5)));
        studentList.add(new Student(7, "e", "三中", "二年级", 15, new BigDecimal(5)));
    }

    // 统计每个学校的里学生年龄不为空并且年龄在9到16岁间的合计分数，并且获取合计分前2名的学校
    public static void main(String[] args) {

    // 等价于SQL:
    // select school,sum(score)
    // from students
    // where age is not null and age >=9 and age <= 16
    // group by school
    // order by sum(score) desc
    // limit 2
        SDFrame<FI2<String, BigDecimal>> sdf2 = SDFrame.read(studentList)
                .whereNotNull(Student::getAge)
                .whereBetween(Student::getAge, 9, 16)
                .groupBySum(Student::getSchool, Student::getScore)
                .sortDesc(FI2::getC2)
                .cutFirst(2)
                ;
        sdf2.show();
    }
}
